package servlet;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import dao.UserDAO;
import entity.User;

/**
 * Servlet implementation class Register
 */
@WebServlet("/Register")
public class Register extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private UserDAO userDAO = UserDAO.getInstance();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Register() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();

		

			String user_password =  request.getParameter("password");
			String user_name = request.getParameter("username");
			String sex = request.getParameter("sex");
			String user_email = request.getParameter("useremail");
			int user_islocked = 0;
			String user_tel = request.getParameter("usertel");
			
			int user_sex;
			if(sex.equals("男")) {
				 user_sex=1;}
			else { user_sex=0;}
			
			
			User user = new User( user_password, user_name, user_sex, user_islocked, user_email,
					user_tel) ;
				
			int user_id = userDAO.addUser(user);
			request.setAttribute("user_id",user_id);
			//request.re("UserLogin.jsp").forward(request, response);
			response.sendRedirect("UserLogin.jsp?user_id="+user_id);
			//System.out.println(user_password);
			//System.out.println(user_name);
			//System.out.println(sex);
			/*System.out.println("user_email");
			System.out.println("user_islocked");
			System.out.println("user_tel");*/	
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
